{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.5705029964447021\tb2839c2dc67bbaf3570b43b6c413529bf5125aa9\n",
      "0.6093711853027344\tb2839c2dc67bbaf3570b43b6c413529bf5125aa9\n",
      "指定超过文件大的数读取比正常的慢：6.81%\n"
     ]
    }
   ],
   "source": [
    "import hashlib\n",
    "import time\n",
    "\n",
    "# read() 指定大小过大会导致时间延长，但不明显，且不会改变hash值\n",
    "s = time.time()\n",
    "sha1 = hashlib.sha1()\n",
    "for i in range(100):\n",
    "    sha1.update(open(\"test.jpeg\", \"rb\").read(19092100))\n",
    "t1 = time.time() - s\n",
    "print(t1, end=\"\\t\")\n",
    "print(sha1.hexdigest())\n",
    "\n",
    "s = time.time()\n",
    "sha1 = hashlib.sha1()\n",
    "for i in range(100):\n",
    "    sha1.update(open(\"test.jpeg\", \"rb\").read(19092100+10000))\n",
    "t2 = time.time() - s\n",
    "print(t2, end=\"\\t\")\n",
    "print(sha1.hexdigest())\n",
    "print(\"指定超过文件大的数读取比正常的慢：%0.2f%%\" % ((t2-t1)*100/t1) )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "ename": "",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m无法启动 Jupyter。试图定位jupyter时出错：在解释器 Python 3.8.9 64-bit 中没有安装数据科学库 notebook 和 jupyter。\n",
      "Run the following command to install 'jupyter 和 notebook' into the Python environment. \n",
      "Command: '/usr/bin/python3 -m pip install jupyter notebook -U'"
     ]
    }
   ],
   "source": [
    "import os\n",
    "\n",
    "# md5所用的时间比sha1要长\n",
    "s = time.time()\n",
    "sha1 = hashlib.sha1()\n",
    "for i in range(100):\n",
    "    sha1.update(open(\"test.jpeg\", \"rb\").read(19092100))\n",
    "t1 = time.time() - s\n",
    "print(t1, end=\"\\t\")\n",
    "print(sha1.hexdigest())\n",
    "\n",
    "s = time.time()\n",
    "md5 = hashlib.md5()\n",
    "for i in range(100):\n",
    "    md5.update(open(\"test.jpeg\", \"rb\").read(19092100))\n",
    "t2 = time.time() - s\n",
    "print(t2, end=\"\\t\")\n",
    "print(md5.hexdigest())\n",
    "print(\"md5比sha1慢：%0.2f%%\" % ((t2-t1)*100/t1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.23168015480041504\td22595974538345167c8712361dac319bcc6637e\n",
      "0.31119847297668457\td22595974538345167c8712361dac319bcc6637e\n",
      "探测文件大小再读取比不探时间长34.32%\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import hashlib\n",
    "\n",
    "s = time.time()\n",
    "sha1 = hashlib.sha1()\n",
    "for i in range(1000):\n",
    "    f = open(\"test.jpeg\", \"rb\")\n",
    "    f.seek(100*1024)\n",
    "    sha1.update(f.read(100*1024))\n",
    "t1 = time.time() - s\n",
    "print(t1, end=\"\\t\")\n",
    "print(sha1.hexdigest())\n",
    "\n",
    "s = time.time()\n",
    "sha1 = hashlib.sha1()\n",
    "for i in range(1000):\n",
    "    if os.stat(\"test.jpeg\").st_size > 200 * 1024:\n",
    "        f = open(\"test.jpeg\", \"rb\")\n",
    "        f.seek(100*1024)\n",
    "        sha1.update(f.read(100*1024))\n",
    "t2 = time.time() - s\n",
    "print(t2, end=\"\\t\")\n",
    "print(sha1.hexdigest())\n",
    "print(\"探测文件大小再读取比不探时间长%0.2f%%\" % ((t2-t1)*100/t1) )"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "69bd962f501c29508fe434b835928c9b89fb5a50a44ab722852ffcc7148156fc"
  },
  "kernelspec": {
   "display_name": "Python 3.6.3 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
